package com.bpmn.system.controller;

import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.apikey.annotation.SaCheckApiKey;
import cn.dev33.satoken.apikey.model.ApiKeyModel;
import cn.dev33.satoken.apikey.template.SaApiKeyUtil;
import com.bpmn.common.entity.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * API Key 资源 相关接口
 *
 * @author rain
 */
@RestController
@Tag(name = "API Key 资源接口", description = "受 API Key 保护的资源访问接口")
@RequestMapping("/system/akRes")
public class ApiKeyResourcesController {

    // 必须携带有效的 ApiKey 才能访问
    @SaCheckApiKey
    @RequestMapping("/akRes1")
    @Operation(summary = "资源接口 1", description = "必须携带有效的 API Key 访问")
    public ResponseResult<String> akRes1() {
        ApiKeyModel akModel = SaApiKeyUtil.currentApiKey();
        System.out.println("当前 ApiKey: " + akModel);
        return ResponseResult.success("调用成功");
    }

    // 必须携带有效的 ApiKey ，且具有 userinfo 权限
    @SaCheckApiKey(scope = "userinfo")
    @RequestMapping("/akRes2")
    @Operation(summary = "资源接口 2", description = "必须携带有效的 API Key 并拥有 userinfo 权限")
    public ResponseResult<String> akRes2() {
        ApiKeyModel akModel = SaApiKeyUtil.currentApiKey();
        System.out.println("当前 ApiKey: " + akModel);
        return ResponseResult.success("调用成功");
    }

    // 必须携带有效的 ApiKey ，且同时具有 userinfo、chat 权限
    @SaCheckApiKey(scope = {"userinfo", "chat"})
    @RequestMapping("/akRes3")
    @Operation(summary = "资源接口 3", description = "必须携带有效的 API Key 并同时拥有 userinfo 和 chat 权限")
    public ResponseResult<String> akRes3() {
        ApiKeyModel akModel = SaApiKeyUtil.currentApiKey();
        System.out.println("当前 ApiKey: " + akModel);
        return ResponseResult.success("调用成功");
    }

    // 必须携带有效的 ApiKey ，且具有 userinfo、chat 其中之一权限
    @SaCheckApiKey(scope = {"userinfo", "chat"}, mode = SaMode.OR)
    @RequestMapping("/akRes4")
    @Operation(summary = "资源接口 4", description = "必须携带有效的 API Key 并拥有 userinfo 或 chat 权限")
    public ResponseResult<String> akRes4() {
        ApiKeyModel akModel = SaApiKeyUtil.currentApiKey();
        System.out.println("当前 ApiKey: " + akModel);
        return ResponseResult.success("调用成功");
    }
}
